package com.park.fristclass;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;
    private static Integer CODE=10010;
    private static ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Button button=findViewById(R.id.camera);

        imageView=findViewById(R.id.image);
//        intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
//            @Override
//            public void onActivityResult(ActivityResult result) {
//                //此处是跳转的result回调方法
//                Intent data = result.getData();
//                Bundle extras = data.getExtras();
//                Bitmap bitmap = (Bitmap) extras.get("data");
//                imageView.setImageBitmap(bitmap);
//
//                Toast.makeText(getApplicationContext(),"从相机返回", Toast.LENGTH_LONG).show();
//            }
//        });


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                takePhoto();
            }
        });


    }

    /**
     * 拍摄照片
     */
    private void takePhoto(){
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//用来打开相机的Intent

        startActivityForResult(takePhotoIntent,CODE);
//        intentActivityResultLauncher.launch(takePhotoIntent);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode==CODE){
            Toast.makeText(getApplicationContext(),"从相机返回", Toast.LENGTH_LONG).show();

                Bundle extras = data.getExtras();
                Bitmap bitmap = (Bitmap) extras.get("data");
                imageView.setImageBitmap(bitmap);
        }

        Log.e("返回", String.valueOf(requestCode));
        Log.e("返回", String.valueOf(resultCode));
        super.onActivityResult(requestCode, resultCode, data);
    }
}